<template>
	<view class="main">

		<view class="msg_list" v-if="sho">
			<view class="msg_list_tit">
				<text class="msg_li_d t1">订 单 号：{{tradeId}}</text>
				<text class="msg_li_d t1">购买时间：{{createdTime}}</text>
				<text class="msg_li_d t1">店铺名称：{{dpname}}</text>
				<text class="msg_li_d t1">店铺所属地区：{{adrdp}}</text>
				<text class="msg_li_d t1">店铺地址：{{adradr}}</text>
				<view class="t1 msg_li_d">
					<view class="t1 msg_li_di">核销状态：</view>
					<view class="t1 msg_li_di" v-if="status==2">待核销</view>
					<!-- <view class="hexiao t3" v-if="status==2" @click="heixiao">核销</view> -->
				</view>
				<view class="msg_list_img">
					<image src="../../static/img/pro/11.jpg" mode="aspectFit"></image>
				</view>
				<text class="msg_li_d t1" style="width: 60vw;margin-left: 4vw;border: none;">
					<text class="t1 msg_li_di" style="width: 100%;">{{name}}</text>
					<text class="t11 msg_li_di">￥{{amount/100}}</text>
					<text class="t1 msg_li_di" style="margin-left: 1vw;">x {{num}}</text>
				</text>
			</view>
			<view class="login_main_but t9" @click="hexiao()">确认核销</view>
		<view class="msg_list_tit t1" v-if="tishi!=''">错误提示:adId{{tishi}}</view>
		</view>
		<view v-show="wei">
			<view class="zanwu">
				<image src="../../static/img/zanwu.png" mode="widthFix"></image>
				<text class="t8">没有扫数据哦</text>
				<text class="t8">{{cuo}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				mainid: '',
				qr: '',
				sho: false,
				wei: false,
				cuo: '',
				tradeId: '',
				status: '',
				adId: '',
				num: '',
				picUrl: '',
				createdTime: '',
				name: '',
				amount: '',
				adrdp: '',
				adradr: '',
				dpname: '',
				tishi:'',
			}
		},
		onLoad() {
			var _this = this;
			uni.getStorage({
				key: 'user',
				success: function(res) {
					_this.mainid = res.data.id;

					setTimeout(function() {
						// 只允许通过相机扫码
						try{
							uni.scanCode({
								onlyFromCamera: true,
								success: function(res) {
									console.log('条码内容：' + res.result);
							
									_this.qr = res.result;
									// _this.sho=true;
							
									_this.info();
								},
								fail: function(res) {
									console.log(res);
									if (res.errMsg == "scanCode:fail cancel") {
										_this.wei = true
										// _this.qr = '1419954283131039745';
										_this.info();
									}
								}
							});
						}catch(e){
							//TODO handle the exception
						}
					}, 500);
				}
			});
		},
		methods: {

			info() {
				var _this = this;
				console.log(_this.mainid);
				console.log(_this.qr);
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'ad/getCheckTrade', //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						"payeeId": _this.mainid,
						"tradeId": _this.qr,
					},
					success: (res) => {
						console.log(res);
						if (res.data.status == '200') {
							_this.tradeId = res.data.data.tradeId;
							_this.status = res.data.data.status;
							_this.adId = res.data.data.adId;
							_this.num = res.data.data.num;
							_this.picUrl = res.data.data.picUrl;
							_this.createdTime = res.data.data.createdTime;
							_this.name = res.data.data.name;
							_this.amount = res.data.data.amount;
							_this.sho = true;
							_this.wei = false;

							_this.adxq();
						} else {
							_this.wei = true;
							_this.sho = false;
							console.log(res.data.msg);
							_this.cuo = res.data.msg;
							console.log(_this.cuo);
						}
					},
					fail: (error) => {
						console.log(error)
					}
				});
			},
			adxq() {
				var _this = this;
				uni.request({
					header: {
						'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
					},
					url: getApp().globalData.url + 'ad/getAdSell/' + _this.adId, //仅为示例，并非真实接口地址。
					method: 'GET',
					data: {
						id: _this.adId, //	用户ID（mainId）		true	string
					},
					success: (res) => {
						console.log(res);
						if (res.data.status == '200') {
							_this.adradr = res.data.data.addressVO.address+res.data.data.addressVO.name;
							_this.adrdp = res.data.data.addressVO.province.name + res.data.data.addressVO.city
								.name + res.data.data.addressVO.area.name + res.data.data.addressVO.street
								.name;
							_this.dpname = res.data.data.mchName;

						}
					},
					fail: (error) => {
						console.log(error)
					}
				});
			},
			hexiao() {
				var _this = this;
				console.log(_this.mainid, _this.qr);
				uni.request({
					header: {
						'Content-Type': 'application/json', //自定义请求头信息
					},
					url: getApp().globalData.url + 'ad/checkTrade', //仅为示例，并非真实接口地址。
					method: 'POST',
					data: {
						"payeeId": _this.mainid,
						"tradeId": _this.qr,
					},
					success: (res) => {
						console.log(res);
						uni.hideLoading(); //关闭加载动画
						if (res.data.status == '200') {
							uni.showToast({
								title: '核销成功',
								icon: 'none',
								duration: 2000
							});
							setTimeout(function() {
								uni.navigateBack({
									delta: 1
								})
							}, 2000)
						} else {
							_this.tishi=res.data.msg
							uni.showToast({
								title: res.data.msg,
								icon: 'none',
								duration: 2000
							});
						}
					},
					fail: (error) => {
						uni.hideLoading();
						// this.checks = true
						uni.showToast({
							title: error,
							icon: 'none',
							duration: 2000
						});
						console.log(error)
					}

				});
			},
		}
	}
</script>

<style>
	.msg_li_d {
		height: auto;
		line-height: 8vw;
		width: 100%;
		float: left;
		padding: 2vw 0;
		border-bottom: 1px solid #F2F2F2;
	}

	.msg_li_di {
		width: auto;
		height: 8vw;
		float: left;
	}

	.msg_list {
		width: 100%;
		height: auto;
		display: block;
		float: left;
		box-shadow: 0px 0px 5px #eee;
		padding: 2vw 3vw;
		border-radius: 2vw;
		/* border: 1px solid #F2F2F2; */
		margin-top: 4vw;
	}

	.msg_list_tit {
		width: 100%;
		height: auto;
		display: block;
		float: left;
		padding: 1vw 0;
	}

	.msg_list_img {
		width: 20vw;
		height: 20vw;
		display: block;
		float: left;
		margin: 3vw 0;
	}

	.msg_list_img image {
		width: 100%;
		height: 100%;
	}


	/* .msg_list_tit text {
		width: 100%;
		height: 12vw;
		line-height: 12vw;
		display: block;
		float: left;
		overflow: hidden;
		border-bottom: 1px solid #F2F2F2;
	} */


	.login_main_but {
		width: 60vw;
		height: 13vw;
		display: block;
		float: left;
		margin: 10vw 15vw;
		line-height: 13vw;
		text-align: center;
		background: #f1c75e;
		border-radius: 6.5vw;
		background-size: 100%;
	}
</style>
